package com.aopcinemas.visitor.behaviour;

import jade.core.behaviours.Behaviour;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.aopcinemas.cinema.model.Schedule;
import com.aopcinemas.visitor.VisitorAgent;

public class ScheduleInfoNotifyBehavior extends Behaviour {

    private static final long serialVersionUID = 1L;

    private List<Schedule> schedules = new ArrayList<Schedule>();
    private int count = 0;
    private int current = 0;

    public ScheduleInfoNotifyBehavior(int count) {
        this.count = count;
    }

    public void addSchedules(List<Schedule> schedules) {
        current++;
        if (schedules != null) {
            this.schedules.addAll(schedules);
        }
    }

    @Override
    public void action() {
        if (current == count) {
            HashMap<String, Schedule> schedulesMap = new HashMap<String, Schedule>();
            for (Schedule schedule : schedules) {
                Schedule s = schedulesMap.get(schedule.getMovie().getName());
                if (s == null || schedule.getBasePrice() < s.getBasePrice()) {
                    schedulesMap.put(schedule.getMovie().getName(), schedule);
                }
            }
            schedulesMap.values();

            ((VisitorAgent) myAgent).notifySchduleInfo(new ArrayList<Schedule>(schedulesMap.values()));
        }
    }

    @Override
    public boolean done() {
        return current == count;
    }

}
